package com.example.itshome.network;

import android.app.Activity;
import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Objects;

public class RegisterRequest extends AbstractNetworkRequest{

    private String verifyCode;
    private String nickName;
    private String password;
    private String passwordConfirm;
    private String email;
    private String username;

    public RegisterRequest setUsername(String username) {
        this.username = username;
        return this;
    }

    public RegisterRequest setVerifyCode(String verifyCode) {
        this.verifyCode = verifyCode;
        return this;
    }

    public RegisterRequest setNickName(String nickName) {
        this.nickName = nickName;
        return this;
    }

    public RegisterRequest setPassword(String password) {
        this.password = password;
        return this;
    }

    public RegisterRequest setPasswordConfirm(String passwordConfirm) {
        this.passwordConfirm = passwordConfirm;
        return this;
    }

    public RegisterRequest setEmail(String email) {
        this.email = email;
        return this;
    }

    public RegisterRequest(Activity activity) {
        super(activity);
    }

    @Override
    protected String getPathAndParams() {
        return "user/register?username="+username+
                "&password="+password+
                "&email="+email+
                "&nickname="+nickName+
                "&verify-code="+verifyCode;
    }

    @Override
    protected void handleResult(String result) {
        try {
            JSONObject jsonObject=new JSONObject(result);
            int code=jsonObject.getInt("code");
            if(code!=200){
                return;
            }
            setHandleResultRunnable(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(activity,"注册成功",Toast.LENGTH_SHORT).show();
                    activity.finish();
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    @Override
    protected boolean canStart() {
        if (!Objects.equals(password, passwordConfirm)){
            Toast.makeText(activity,"两次密码输入不一致",Toast.LENGTH_SHORT).show();
            return false;
        }

        return true;
    }
}
